/* QAD_Item-SiteMasterReport.p - Item-Site Master Report                      */
/* Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Id:: QAD_TransactionsAccountingReport.p 1046 2010-09-10 09:08:19Z jvz  $: */
/*                                                                            */
/* Report matches legacy ppptiq00.p for use by QAD Reporting Framework.       */
/*                                                                            */

{mfsubdirs.i}
{{&US_BBI}mfdeclre.i}
{{&US_BBI}gplabel.i}

{com/qad/shell/report/dsReportRequest.i}
{com/qad/shell/report/ReportConstants.i}

/*============================================================================*/
/* ****************************  TEMP TABLES  ******************************* */
/*============================================================================*/

/* TEMP-TABLE DEFINITION BLOCK */

define temp-table tt_pt_mstr no-undo like pt_mstr
   field alt_part             like ptal_mstr.ptal_alt_part
   field ptrev                like ptrv_det.ptrv_rev
   field issuePolicyDisp      as   character
   field pickPolicyDisp       as   character
   field ptPickOrder          like pt_mstr.pt_pk_ord
   field ptPickAscend         like pt_mstr.pt_ascend
   field l_comm_code          like comd_det.comd_comm_code
   field replenishment_method as character
   field mfg_lead             like ptp_det.ptp_mfg_lead
   field pur_lead             like ptp_det.ptp_pur_lead
   field schedType            as   character
   field smoothingRule        as   character
   field periodType           as   character
   field ptavgperiod          as   character
   field c_fiscal_desc        as   character
   field unique_id            as   integer
.

define temp-table ttCostInfo no-undo
   field element    like spt_det.spt_element
   field cst_tl     like spt_det.spt_cst_tl
   field cst_ll     like spt_det.spt_cst_ll
   field is_primary as   logical
   field cat_desc   as   character
   field ao         like spt_det.spt_ao
   field unique_id  as   integer
   field ref_id     like tt_pt_mstr.unique_id
   field total_id  as   integer
.

define temp-table ttTotal no-undo
   field tot_tl    like spt_det.spt_cst_tl
   field tot_ll    like spt_det.spt_cst_ll
   field cst_date  like sct_det.sct_cst_date
   field site      like pt_mstr.pt_site
   field csset     as   character
   field ref_id    like ttCostInfo.unique_id
.

define dataset dsReportResults for tt_pt_mstr, ttCostInfo, ttTotal.

/*============================================================================*/
/* *****************************  MAIN BLOCK  ******************************* */
/*============================================================================*/
define input parameter runReport    as logical no-undo.
define input parameter reportHandle as handle  no-undo.
define input parameter dataset      for dsReportRequest.
define output parameter dataset-handle phReportResults.

{com/qad/shell/report/reporting.i}

define variable vhDS           as handle    no-undo.
define variable unique_id_part as integer no-undo.
define variable unique_id_cost as integer no-undo.
define workfile sptwkfl
      field element  like spt_element 
      field primary  like mfc_logical 
      field prim2    like mfc_logical
      field ao       like spt_ao     
      field cst_tl   like spt_cst_tl
      field cst_ll   like spt_cst_ll
      field cst_tot  like spt_cst_tl
      field cat_desc as character   
      field seq      like spt_pct_ll
      field part     like pt_part.

empty temp-table tt_pt_mstr no-error.
empty temp-table ttCostInfo no-error.
empty temp-table ttTotal    no-error.

for first ttReportRequest no-lock:

   vhDS = dataset dsReportResults:handle.
   run FillMetaData in reportHandle(
      vhDS, "QAD_Item-SiteMasterReport.meta.xml").

   run SetReportMetaData (input reportHandle).

   if runReport then do:
      run RunReport
         (output dataset-handle phReportResults).
   end.

   delete object phReportResults no-error.

end.

PROCEDURE SetReportMetaData:

   define input parameter  ipReportHandle as handle.

   run SetFieldMetaParameter in ipReportHandle(
         "tt_pt_mstr", "pt_site", "DefaultValue", global_site).
   
   run SetFieldMetaParameter in ipReportHandle(
         "tt_pt_mstr", "pt_part", "DefaultValue", global_part).

END PROCEDURE. /* PROCEDURE ModifyMetaData */

/* DATA RETRIEVING LOGIC BLOCK */
PROCEDURE RunReport:

   define output parameter dataset-handle phReportResults.

   /* RETRIEVE THE DATA FROM DATABASE */
   define variable queryString   as character no-undo.
   define variable hMainQuery    as handle    no-undo.
   define query    MainQuery        for pt_mstr, si_mstr, in_mstr.
   define query    MainQueryWithAlt for ptal_mstr, pt_mstr, si_mstr, in_mstr.


   assign
      hMainQuery           = query MainQuery:handle
      hSubQuery            = query SubQuery:handle
   .
   queryString = "for each pt_mstr no-lock where pt_domain = "
                   + quoter(global_domain).

   if not IsConditionEmpty("tt_pt_mstr", "pt_site")
   then do:
      assign
         hMainQuery      = query MainQueryWithAlt:handle
         queryString     = "for each ptal_mstr no-lock "
                         + " where ptal_mstr.ptal_domain = "
                         + quoter(global_domain)
                         + " and ptal_mstr.ptal_active ".

      run AddAllConditions in reportHandle
         ( "ptal_mstr" , input-output queryString ).

      queryString = queryString + " , each pt_mstr no-lock where pt_domain = "
                      + quoter(global_domain)
                      + " and pt_part = ptal_part ".
   end. /* if not IsConditionEmpty ... */

   run AddSomeConditions in reportHandle
      ( "pt_mstr", "pt_site", input-output queryString ).

   queryString = queryString + ", first si_mstr "
               + " where si_mstr.si_domain = "
               + quoter(global_domain).

   run AddSpecificConditions in reportHandle
      ( "pt_mstr", "pt_site:si_site", input-output queryString ).

   queryString = queryString + " , each in_mstr no-lock where in_domain = "
                     + quoter(global_domain)
                     + " and in_part = pt_part ".
   
   run AddSpecificConditions in reportHandle
         ("pt_mstr", "pt_site:in_site", input-output queryString).

   queryString = queryString + ":".

   hMainQuery:query-prepare(queryString).
   hMainQuery:query-open().
   hMainQuery:get-next().

   repeat while not hMainQuery:query-off-end:
      
      create tt_pt_mstr.
      buffer-copy pt_mstr to tt_pt_mstr no-error.
      
      assign
         unique_id_part          = unique_id_part + 1
         tt_pt_mstr.unique_id    = unique_id_part
      .
      
      run GetRevisionNumber(output tt_pt_mstr.ptRev).

      run FillItemInventoryData.

      run GetCorpCommodityCode(output tt_pt_mstr.l_comm_code).
     
      run FillItemPlanningData.

      run FillSmoothingRulesData. 
   
      run FillPurchaseOrderData.

      run FillPriceData.
   
      hMainQuery:get-next().
   end. /* REPEAT QUERY */

   hMainQuery:query-close().
   delete object hMainQuery.
   phReportResults = dataset dsReportResults:handle.
   delete object phReportResults no-error.


END PROCEDURE.

PROCEDURE GetRevisionNumber:
   
   define output parameter ptRev as character no-undo.

   {us/px/pxgetph.i pprevxr}
   ptRev = {us/px/pxfunct.i &FUNCTION='getValidItemRevision'
      &PROGRAM='pprevxr.p'
      &HANDLE=ph_pprevxr
      &PARAM= "input pt_mstr.pt_part,
        input '',
        input today"}.
   /*  REVISION NUMBER IS DISPLAYED FROM PTP_DET TO GET LATEST  */
   /*  MODIFIED REVISION NUMBER                                 */
   for first ptp_det
      where ptp_det.ptp_domain = global_domain and  ptp_part = pt_part
      and   ptp_site = in_mstr.in_site
   no-lock:
   end. /* FOR FIRST PTP_DET */

   if available ptp_det then
   do:
      {us/px/pxgetph.i pprevxr}
      ptpRev = {us/px/pxfunct.i &FUNCTION='getValidItemRevision'
         &PROGRAM='pprevxr.p'
         &HANDLE=ph_pprevxr
         &PARAM= "input ptp_part,
           input in_mstr.in_site,
           input today"}.
   end.

END PROCEDURE.

PROCEDURE FillItemInventoryData:
   
   define variable itemIssuePolicy     like pti_det.pti_iss_pol no-undo.
   define variable issuePolicyDisp     as character no-undo.
   define variable issuePolicyDispDesc as character no-undo.
   define variable itemPickPolicy      like pti_det.pti_pick_pol no-undo.
   define variable pickPolicyDisp      as character no-undo.
   define variable pickPolicyDispDesc  as character no-undo.
   define variable pickingOrder        as character no-undo.
   define variable pickingAscDesc      as character no-undo.
   define variable ptPickOrder         like pt_mstr.pt_pk_ord no-undo.
   define variable ptPickAscend        like pt_mstr.pt_ascend no-undo.
   define variable l_comm_code         like comd_det.comd_comm_code no-undo.
   /* get picking order */
   {us/px/pxrun.i &PROC  = 'getFieldDefault' &PROGRAM = 'ppitxr.p'
      &HANDLE = ph_ppitxr
      &PARAM = "(input pt_part,
        input site,
        input ""pt_pk_ord"",
        output pickingOrder)"
      &NOAPPERROR = true
      &CATCHERROR = true}
   assign ptPickOrder = integer(pickingOrder).

   /* get picking order */
   {us/px/pxrun.i &PROC  = 'getFieldDefault' &PROGRAM = 'ppitxr.p'
      &HANDLE = ph_ppitxr
      &PARAM = "(input pt_part,
        input site,
        input ""pt_ascend"",
        output pickingAscDesc)"
      &NOAPPERROR = true
      &CATCHERROR = true}
   assign ptPickAscend = logical(pickingAscDesc = "yes").

   /* get item issue policy */
   {us/px/pxrun.i &PROC  = 'getFieldDefault' &PROGRAM = 'ppitxr.p'
      &HANDLE = ph_ppitxr
      &PARAM = "(input pt_part,
        input site,
        input ""pt_iss_pol"",
        output itemIssuePolicy)"
      &NOAPPERROR = true
      &CATCHERROR = true}

   /* GET ISSUE POLICY DISPLAY FLAG FROM lngd_det */
   {us/px/pxrun.i &PROC  ='convertNumericToAlpha'
      &PROGRAM = 'gplngxr.p'
      &HANDLE=ph_gplngxr
      &PARAM = "(input 'wo_mstr',
        input 'issuePolicy',
        input itemIssuePolicy,
        output issuePolicyDisp,
        output issuePolicyDispDesc)"
      &NOAPPERROR = true
      &CATCHERROR = true}

   /* get item pick policy */
   {us/px/pxrun.i &PROC  = 'getFieldDefault' &PROGRAM = 'ppitxr.p'
      &HANDLE = ph_ppitxr
      &PARAM = "(input pt_part,
        input site,
        input ""pt_pick_pol"",
        output itemPickPolicy)"
      &NOAPPERROR = true
      &CATCHERROR = true}

   /* GET ISSUE POLICY DISPLAY FLAG FROM lngd_det */
   {us/px/pxrun.i &PROC  ='convertNumericToAlpha'
      &PROGRAM = 'gplngxr.p'
      &HANDLE=ph_gplngxr
      &PARAM = "(input 'wo_mstr',
        input 'pickPolicy',
        input itemPickPolicy,
        output pickPolicyDisp,
        output pickPolicyDispDesc)"
      &NOAPPERROR = true
      &CATCHERROR = true}

   display {us/pp/pppsmta3.i} with frame b1.
   assign
      tt_pt_mstr.issuePolicyDisp = issuePolicyDisp
      tt_pt_mstr.pickPolicyDisp  = pickPolicyDisp
   
   for first si_mstr where si_domain = global_domain
      and   si_site   = site
   no-lock: 
   end.
   
   for first pti_det 
      where pti_det.oid_pt_mstr = pt_mstr.oid_pt_mstr
      and   pti_det.oid_si_mstr = si_mstr.oid_si_mstr
   no-lock:
      assign
         tt_pt_mstr.pt_abc          = pti_abc   
         tt_pt_mstr.pt_cyc_int      =  pti_cyc_int 
         tt_pt_mstr.pt_avg_int      =pti_avg_int  
         tt_pt_mstr.pt_loc          =  pti_loc      
         tt_pt_mstr.pt_loc_type     =pti_loc_type  
         tt_pt_mstr.pt_shelflife    =pti_shelflife 
         tt_pt_mstr.pt_critical     =pti_critical 
         tt_pt_mstr.pt_memo_type    =pti_memo_type 
         tt_pt_mstr.pt_lot_ser      =pti_lot_ser   
         tt_pt_mstr.pt_auto_lot     =pti_auto_lot  
         tt_pt_mstr.pt_rctwo_status =pti_rctwo_status 
         tt_pt_mstr.pt_rctwo_active =pti_rctwo_active 
         tt_pt_mstr.pt_rctpo_status =pti_rctpo_status 
         tt_pt_mstr.pt_rctpo_active =pti_rctpo_active
      .
   end.

   assign
      tt_pt_mstr.ptPickOrder  = ptPickOrder
      tt_pt_mstr.ptPickAscend = ptPickAscend
   .

END PROCEDURE.


PROCEDURE GetCorpCommodityCode:
   define output parameter l_comm_code as character no-undo.
   
   for first comd_det  where comd_det.comd_domain = global_domain 
      and comd_part = pt_part
   no-lock:
   end.
   
   if available comd_det then
      assign l_comm_code = comd_comm_code.
   else
      assign l_comm_code = "".

END PROCEDURE. /* PROCEDURE GetCorpCommodityCode */

PROCEDURE FillItemPlanningData:

   define variable emt-auto as logical no-undo.
   
   for first ptp_det  where ptp_det.ptp_domain = global_domain 
      and ptp_part = in_part
      and ptp_site = in_site
   no-lock:
   end.

   if available ptp_det then do:

      /* GET DEFAULT BTB TYPE FROM lngd_det */
      {us/gp/gplngn2a.i &file = ""emt""
         &field = ""btb-type""
         &code  = ptp_btb_type
         &mnemonic = btb-type
         &label = btb-type-desc}

      /* GET DEFAULT ATP ENFORCEMENT TYPE FROM lngd_det */
      {us/gp/gplngn2a.i &file = ""atp""
         &field = ""atp-enforcement""
         &code  = ptp_atp_enforcement
         &mnemonic = atp-enforcement
         &label = atp-enforce-desc}

      if ptp__qad02 = 1 then emt-auto = yes.
      
      {us/gp/gplngn2a.i &file = ""rep_mthd2""
                  &field = ""replenishment_method""
                  &code = ptp_replenishment_mthd
                  &mnemonic = replenishment_method
                  &label = replenishment_method_label}
      if ptp__qad02 = 1 then emt-auto = yes.

      assign
         tt_pt_mstr.pt_ms        = ptp_ms        
         tt_pt_mstr.pt_buyer     = ptp_buyer       
         tt_pt_mstr.pt_phantom   = ptp_phantom    
         tt_pt_mstr.pt_plan_ord  = ptp_plan_ord   
         tt_pt_mstr.pt_vend      = ptp_vend       
         tt_pt_mstr.pt_ord_min   = ptp_ord_min     
         tt_pt_mstr.pt_timefence = ptp_timefnce    
         tt_pt_mstr.pt_po_site   = ptp_po_site    
         tt_pt_mstr.pt_ord_max   = ptp_ord_max    
         tt_pt_mstr.pt_mrp       = in_mrp          
         tt_pt_mstr.pt_pm_code   = ptp_pm_code        
         tt_pt_mstr.pt_ord_mult  = ptp_ord_mult    
         tt_pt_mstr.pt_ord_pol   = ptp_ord_pol   
         tt_pt_mstr.pt_cfg_type  = cfg            
         tt_pt_mstr.pt_op_yield  = ptp_op_yield    
         tt_pt_mstr.pt_ord_qty   = ptp_ord_qty     
         tt_pt_mstr.pt_insp_rqd  = ptp_insp_loc     
         tt_pt_mstr.pt_yield_pct = ptp_yld_pct     
         tt_pt_mstr.pt_batch     = ptp_batch       
         tt_pt_mstr.pt_insp_rqd  = ptp_ins_rqd     
         tt_pt_mstr.pt_run       = ptp_run         
         tt_pt_mstr.pt_ord_per   = ptp_ord_per     
         tt_pt_mstr.pt_insp_lead = ptp_ins_lead    
         tt_pt_mstr.pt_cum_lead  = ptp_cum_lead    
         tt_pt_mstr.pt_setup     = ptp_setup       
         tt_pt_mstr.pt_sfty_stk  = ptp_sfty_stk    
         tt_pt_mstr.ptp_mfg_lead = ptp_mfg_lead    
         tt_pt_mstr.ptp_pur_lead = ptp_pur_lead    
         tt_pt_mstr.pt_btb_type  = btb-type        
         tt_pt_mstr.pt_sfty_time = ptp_sfty_tme    
         tt_pt_mstr.pt_atp_enforcement
                                 = atp-enforcement 
         tt_pt_mstr.pt__qad15    = emt-auto        
         tt_pt_mstr.pt_rop       = ptp_rop         
         tt_pt_mstr.pt_atp_family= ptp_atp_family  
         tt_pt_mstr.pt_network   = ptp_network     
         tt_pt_mstr.ptRev        = ptpRev    
         tt_pt_mstr.pt_atp_horizon  
                                 = ptp_atp_horizon 
         tt_pt_mstr.pt_routing   = ptp_routing     
         tt_pt_mstr.pt_run_seq1  = ptp_run_seq1    
         tt_pt_mstr.pt_bom_code  = ptp_bom_code    
         tt_pt_mstr.pt_run_seq2  = ptp_run_seq2    
         tt_pt_mstr.replenishment_method 
                                 = replenishment_method
      .

      if ptp_bom_code <> ""
      then do:
         for first bom_mstr no-lock
             where bom_mstr.bom_domain = global_domain 
             and  bom_parent = ptp_bom_code:
         end.
      end.
      else do:
         for first bom_mstr no-lock
             where bom_mstr.bom_domain = global_domain 
             and  bom_parent = ptp_part:
         end.
      end.
      assign
         tt_pt_mstr.pt_batch = bom_batch 
               when (available bom_mstr and bom_batch <> 0)
         tt_pt_mstr.pt_batch = 1 
               when ( not available bom_mstr 
                  or (available bom_mstr and bom_batch = 0))
      .
      
      /* DISPLAY CONFIGURATION TYPE */
      /* GET MNEMONIC cfg AND cfglabel FROM LNGD_DET */
      assign
         cfg = ptp_cfg_type
         cfglabel = ""
         cfgcode = "".

      {us/gp/gplngn2a.i &file     = ""pt_mstr""
         &field    = ""pt_cfg_type""
         &code     = ptp_cfg_type
         &mnemonic = cfg
         &label    = cfglabel}
      tt_pt_mstr.pt_cfg_type = cfg.
   end.
   else do:

      /* GET DEFAULT ATP ENFORCEMENT TYPE FROM lngd_det */
      {us/gp/gplngn2a.i &file = ""atp""
         &field = ""atp-enforcement""
         &code  = pt_atp_enforcement
         &mnemonic = atp-enforcement
         &label = atp-enforce-desc}

      /* GET DEFAULT BTB TYPE FROM lngd_det */
      {us/gp/gplngn2a.i &file = ""emt""
         &field = ""btb-type""
         &code  = pt_btb_type
         &mnemonic = btb-type
         &label = btb-type-desc}
      
      {us/gp/gplngn2a.i &file = ""rep_mthd2""
                  &field = ""replenishment_method""
                  &code = pt_replenishment_mthd
                  &mnemonic = replenishment_method
                  &label = replenishment_method_label}

      assign
         tt_pt_mstr.pt_cfg_type = cfg
         pt_btb_type            = btb-type
         pt_atp_enforcement     = atp-enforcement
         tt_pt_mstr.replenishment_method
                                = replenishment_method
         tt_pt_mstr.pt_mrp      = in_mrp
      .

      if pt_bom_code <> "" 
      then do:
         for first bom_mstr no-lock
            where bom_mstr.bom_domain = global_domain 
            and  bom_parent = pt_bom_code:
         end.
      end.
      else do:
         for first bom_mstr no-lock
             where bom_mstr.bom_domain = global_domain 
             and  bom_parent = pt_part:
         end.
      end.
      
      assign
         tt_pt_mstr.pt_batch = bom_batch
               when (available bom_mstr and bom_batch <> 0)
         tt_pt_mstr.pt_batch = 1 
               when ( not available bom_mstr
               or (available bom_mstr and bom_batch = 0))
      .

      /* DISPLAY CONFIGURATION TYPE */
      assign
         cfg = pt_cfg_type
         cfglabel = ""
         cfgcode = "".
      /* GET MNEMONIC cfg AND cfglabel FROM LNGD_DET */
      {us/gp/gplngn2a.i &file     = ""pt_mstr""
         &field    = ""pt_cfg_type""
         &code     = pt_cfg_type
         &mnemonic = cfg
         &label    = cfglabel}
      tt_pt_mstr.pt_cfg_type = cfg.
   end. /* else not available ptp_det */

END PROCEDURE. /* PROCEDURE FillItemPlanningData */



PROCEDURE FillSmoothingRulesData:
   
   define variable schedType     like pt_schedule_type no-undo.
   define variable smoothingRule like pt_smoothing_rule no-undo.
   define variable periodType    like pt_period_type no-undo.
   define variable cdesc         as character no-undo.
   define variable ptavgperiod   like pt_avg_period no-undo.
   define variable tmpSmoothingRule like pt_smoothing_rule no-undo.
   define variable tmpPeriodType  like pt_period_type no-undo.
   
   for first ptp_det  where ptp_det.ptp_domain = global_domain 
      and ptp_part = in_part
      and ptp_site = in_site
   no-lock:
   end.

   if available ptp_det
   then do:
      assign
         tmpSmoothingRule = ptp_smoothing_rule
         tmpPeriodType    = ptp_period_type
         schedType        = ptp_schedule_type
         ptavgperiod      = ptp_avg_period.
      .
   end.
   else do:
      assign
         tmpSmoothingRule = pt_mstr.pt_smoothing_rule
         tmpPeriodType    = pt_mstr.pt_period_type
         schedType        = pt_mstr.pt_schedule_type
         ptavgperiod      = pt_mstr.pt_avg_period
      .
   end.

   {us/px/pxrun.i &PROC  ='convertNumericToAlpha'
         &PROGRAM = 'gplngxr.p'
         &HANDLE=ph_gplngxr
         &PARAM = "(input 'MPS',
           input  'mpsos_smoothing_rule',
           input  tmpSmoothingRule,
           output smoothingRule,
           output cdesc)"
         }

   {us/px/pxrun.i &PROC  ='convertNumericToAlpha'
         &PROGRAM = 'gplngxr.p'
         &HANDLE=ph_gplngxr
         &PARAM = "(input 'MPS',
           input  'mpsos_period_type',
           input  tmpPeriodType,
           output periodType,
           output cdesc)"
         }
   
   assign
      tt_pt_mstr.schedType          = schedType      
      tt_pt_mstr.smoothingRule      = smoothingRule
      tt_pt_mstr.periodType         = periodType
      tt_pt_mstr.ptavgperiod        = ptavgperiod
   .

END PROCEDURE. /* PROCEDURE FillSmoothingRulesData */

PROCEDURE FillPurchaseOrderData:
   
   for first ptp_det  where ptp_det.ptp_domain = global_domain 
      and ptp_part = in_part
      and ptp_site = in_site
   no-lock:
   end.
   
   if available ptp_det
   then do:
      assign
         tt_pt_mstr.pt_rev_req         = ptp_rev_req         
         tt_pt_mstr.pt_exp_rev_allowed = ptp_exp_rev_allowed
      .
   end.
   else do:
      assign
         tt_pt_mstr.pt_rev_req         = pt_mstr.pt_rev_req         
         tt_pt_mstr.pt_exp_rev_allowed = pt_mstr.pt_exp_rev_allowed
      .
   end.

END PROCEDURE. /* PROCEDURE FillPurchaseOrderData */
   
   define variable l_curr_set       like in_mstr.in_cur_set no-undo.
   define variable l_gl_set         like in_mstr.in_gl_set no-undo.
   define variable gl_site          like in_mstr.in_gl_cost_site no-undo.
   define variable c_fiscal_desc    as character no-undo.
   define buffer   b-in-mstr        for in_mstr.
   
   if not can-find(first sct_det
      where sct_det.sct_domain = global_domain
      and ((sct_site = in_site or sct_site = in_gl_cost_site)
      and   sct_part = pt_mstr.pt_part))
   then do:
      return.
   end.

   assign
      l_gl_set   = icc_gl_set
      l_curr_set = icc_cur_set.

   if in_gl_set <> "" then l_gl_set = in_gl_set.

   if in_cur_set <> "" then l_curr_set = in_cur_set.

   gl_site = in_gl_cost_site.

   if in_site <> in_gl_cost_site
   then do:
      for first b-in-mstr
         where b-in-mstr.in_domain = global_domain
         and   b-in-mstr.in_site   = in_mstr.in_gl_cost_site
         and   b-in-mstr.in_part   = in_mstr.in_part
      no-lock:

         if b-in-mstr.in_gl_set = ""
         then do:
            l_gl_set = icc_gl_set.
         end.
         else do:
            l_gl_set = b-in-mstr.in_gl_set.
         end.
      end. /* for first b-in-mstr */
   end. /* if in_site <> in_gl_cost_site */

   run CreateCostDetailAndTotal(gl_site, l_gl_set).
   run CreateCostDetailAndTotal(gl_site, l_curr_set).

PROCEDURE FillPriceData:

END PROCEDURE /* PROCEDURE FillPriceData */

PROCEDURE CreateCostDetailAndTotal:
   define input parameter gl_site as character no-undo.
   define input parameter csset   as character no-undo.

   define variable i       as integer no-undo.
   define variable usr_val as character extent 5 no-undo.
   define variable usr_elm like sc_mstr.sc_element extent 5 no-undo.

   for first sct_det
      where sct_det.sct_domain = global_domain
      and sct_sim  = csset
      and sct_part = pt_mstr.pt_part
      and sct_site = gl_site
   no-lock:
   end.

   if available sct_det
   then do:
      /* DISPLAY COST DETAILS */
      for each lngd_det no-lock
         where lngd_dataset = "sc_mstr"
         and lngd_key1 = ""
         and lngd_key2 = ""
         and lngd_key3 = ""
         and lngd_field = "sc_category"
         and lngd_lang = global_user_lang:

         if lngd_key4 = "1"
         then do:
            usr_val[1] = lngd_translation.
         end.
         assign
            usr_val[integer(lngd_key4)] = lngd_translation
            usr_elm[integer(lngd_key4)] = substring(lngd_translation,1,8).
      end.

      for each sptwkfl exclusive-lock:
         delete sptwkfl.
      end.

      i = 0.
      for each spt_det no-lock
         where spt_domain = global_domain
           and spt_site   = sct_site
           and spt_sim    = sct_sim
           and spt_part   = sct_part
      by spt_pct_ll by spt_cst_tl descending by spt_element:
         i = i + 1.
         create sptwkfl.
         assign
            element = spt_element
            primary = no
            prim2   = no
            ao      = spt_ao
            cst_tl  = spt_cst_tl
            cst_ll  = spt_cst_ll
            cst_tot = spt_cst_tl + spt_cst_ll
            part    = sct_part
            seq     = i.
         if spt_pct_ll = truncate(spt_pct_ll,0)
         then do:
            assign
               primary = yes
               prim2   = yes.
         end.
         cat_desc = usr_val[integer(truncate(spt_pct_ll,0))].
         release spt_det.
      end.

      find first sptwkfl no-error.
      if not available sptwkfl then do i = 1 to 5:
         create sptwkfl.
         assign
            element  = usr_elm[i]
            primary  = yes
            prim2    = yes
            ao       = no
            cst_tl   = 0
            cst_ll   = 0
            cst_tot  = 0
            part     = sct_part
            seq      = i
            cat_desc = usr_val[i].
      end.

      for each sptwkfl break by part:
         accumulate cst_tl (total by part).
         accumulate cst_ll (total by part).
         accumulate cst_tot (total by part).
      end.

      create ttTotal.
      assign
         unique_id_total      = unique_id_total + 1
         ttTotal.tot_tl       = accum total cst_tl
         ttTotal.tot_ll       = accum total cst_ll
         ttTotal.cst_date     = sct_cst_date
         ttTotal.site         = in_mstr.in_site
         ttTotal.csset        = csset
         ttTotal.ref_id       = unique_id_total.

      for each sptwkfl by seq:
         create ttCostInfo.
         assign
            unique_id_cost       = unique_id_cost + 1
            ttCostInfo.element   = sptwkfl.element
            ttCostInfo.cst_tl    = sptwkfl.cst_tl
            ttCostInfo.is_primary
                                 = sptwkfl.primary
            ttCostInfo.cst_ll    = sptwkfl.cst_ll
            ttCostInfo.ao        = sptwkfl.ao
            ttCostInfo.cat_desc  = sptwkfl.cat_desc
            ttCostInfo.unique_id = unique_id_cost
            ttCostInfo.ref_id    = unique_id_part
            ttCostInfo.total_id  = unique_id_total
         .
      end. /* for each sptwkfl */
   end. /* IF AVAILABLE sct_det ... */
END PROCEDURE. /* PROCEDURE CreateCostDetailAndTotal */
